package com.hubu.parking.service.impl;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hubu.parking.entity.park.ParkingRecord;
import com.hubu.parking.entity.park.ParkingReservation;
import com.hubu.parking.entity.park.ParkingSpot;
import com.hubu.parking.entity.park.User;
import com.hubu.parking.mapper.ParkingReservationMapper;
import com.hubu.parking.model.dto.park.CancelReserveDto;
import com.hubu.parking.model.dto.park.GetCurrentMessageDto;
import com.hubu.parking.model.enums.LogicStatusEnum;
import com.hubu.parking.model.enums.SpotStatusEnum;
import com.hubu.parking.model.enums.UserRoleEnum;
import com.hubu.parking.service.ParkingRecordService;
import com.hubu.parking.service.ParkingReservationService;
import com.hubu.parking.service.ParkingSpotService;
import com.hubu.parking.service.UserService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author 19468
* @description 针对表【parking_reservation(用户车位预约记录表)】的数据库操作Service实现
* @createDate 2025-03-02 16:52:08
*/
@Service
public class ParkingReservationServiceImpl extends ServiceImpl<ParkingReservationMapper, ParkingReservation>
    implements ParkingReservationService{

    @Resource
    @Lazy
    private ParkingRecordService recordService;

    @Resource
    @Lazy
    private ParkingSpotService spotService;

    @Resource
    private UserService userService;

    @Override
    public JSONObject getCurrentMessage(GetCurrentMessageDto dto) {
        if (dto == null) {
            return null;
        }
        Integer userId = dto.getUserId();
        if (userId == null) {
            return null;
        }
        JSONObject res = new JSONObject();
        // 1.判断属于那种情况 未预约 预约 停车中
        // 停车中
        LambdaQueryWrapper<ParkingRecord> recordLambdaQueryWrapper = new LambdaQueryWrapper<>();
        recordLambdaQueryWrapper.eq(ParkingRecord::getUserId, userId);
        recordLambdaQueryWrapper.eq(ParkingRecord::getStatus, LogicStatusEnum.UN_AVAILABLE.getValue());
        List<ParkingRecord> recordList = recordService.list(recordLambdaQueryWrapper);
        if (recordList.size() == 1) {
            ParkingRecord record = recordList.get(0);
            res.put("status", SpotStatusEnum.PARKED.getValue());
            res.put("recordId", record.getRecordId());
            Date startDate = record.getStartTime();
            res.put("startTime", startDate.getTime());
            long endTime = new Date().getTime();
            res.put("duration", (endTime - startDate.getTime()) / 1000);
            res.put("fee", record.getFee());
            return res;
        }
        // 未预约
        LambdaQueryWrapper<ParkingReservation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ParkingReservation::getUserId, userId);
        queryWrapper.eq(ParkingReservation::getStatus, LogicStatusEnum.UN_AVAILABLE.getValue());
        List<ParkingReservation> reservations = this.list(queryWrapper);
        // 防止错误数据
        // 2.1 未预约
        if (reservations.size() != 1) {
            res.put("status", SpotStatusEnum.AVAILABLE.getValue());
            return res;
        }
        ParkingReservation firstReservation = reservations.get(0);
        Integer firstReservationId = firstReservation.getId();
        // 2.2 已预约
        res.put("status", SpotStatusEnum.RESERVED.getValue());
        Date endTime = firstReservation.getEndTime();
        long pass = endTime.getTime();
        long now = new Date().getTime();
        res.put("time", (pass - now) / 1000 / 60);
        Integer spotId = firstReservation.getSpotId();
        ParkingSpot byId = spotService.getById(spotId);
        res.put("spot", byId.getSpotNumber());
        res.put("reservationId", firstReservationId);
        return res;
    }

    @Override
    public Integer cancelReserve(CancelReserveDto dto) {
        if (dto == null) {
            return 0;
        }
        Integer reservationId = dto.getReservationId();
        if (reservationId == null) {
            return 0;
        }
        // 预约信息
        ParkingReservation reservation = this.getById(reservationId);
        Integer userId = reservation.getUserId();
        reservation.setStatus(2);
        this.updateById(reservation);
        // 查询失约次数 >= 3次封禁
        LambdaQueryWrapper<ParkingReservation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ParkingReservation::getStatus, 2);
        queryWrapper.eq(ParkingReservation::getUserId, userId);
        long count = this.count(queryWrapper);
        // 释放车位
        Integer spotId = reservation.getSpotId();
        ParkingSpot byId = spotService.getById(spotId);
        byId.setStatus(SpotStatusEnum.AVAILABLE.getValue());
        spotService.updateById(byId);
        if (count >= 3) {
            User user = userService.getById(userId);
            user.setUserRole(UserRoleEnum.BAN.getValue());
            userService.updateById(user);
            return 2;
        }
        return 1;
    }
}




